﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Threading;
using System.IO;
using devtm.Editor.Configuration.Merge;
using System.Windows;

namespace devtm.Editor.Configuration
{



    public class ConfigurationFileSourceWatcher
    {

        static readonly object watchersLock = new object();
        static readonly object refreshLock = new object();

        public DateTime lastWriteTime;
        private FileInfo _file;
        public ConfigurationFileSourceWatcher(System.IO.FileInfo file)
        {

            _file = file;

            Thread t = new Thread(new ThreadStart(Loop));
            lastWriteTime = File.GetLastWriteTime(file.FullName);

            t.Priority = ThreadPriority.Lowest;

            t.Start();
        }

        public bool quit = false;
        public void Loop()
        {

            do
            {

                try
                {


                    DateTime currentLastWriteTime = File.GetLastWriteTime(_file.FullName);
                    if (currentLastWriteTime != DateTime.MinValue)
                    {
                        if (lastWriteTime.Equals(DateTime.MinValue))
                            lastWriteTime = currentLastWriteTime;

                        else
                            if (lastWriteTime.Equals(currentLastWriteTime) == false)
                                lock (refreshLock)
                                {
                                    lastWriteTime = currentLastWriteTime;
                                    Watcher_ConfigurationChanged();
                                }

                    }

                }

                catch (Exception Ex)
                {
                    Datas.NotifyError("", Ex);
                }

                var test = UCConfigEdit.ConfigEdit;
                Thread.Sleep(500);

            } while (!quit);


            if (!Datas.jobs.Close)
                Datas.jobs.Start(true);

        }


        void Watcher_ConfigurationChanged()
        {

            // List<string> sectionsToNotify = new List<string>();

            lock (watchersLock)
            {

                var ii = new ExeConfigurationFileMap() { ExeConfigFilename = _file.FullName };
                var configuration = ConfigurationManager.OpenMappedExeConfiguration(ii, ConfigurationUserLevel.None);

                try
                {

                    MessageBox.Show("the configuration file has changed, the file must be reloaded.", "Configuration file changed", MessageBoxButton.OK, MessageBoxImage.Warning);

                    quit = true;
                    Datas.IsModified = false;

                    Action j = () => UCConfigEdit.ConfigEdit.Load(_file.FullName);
                    Datas.jobs.Add(() => UCConfigEdit.UniqueDispatcher.BeginInvoke(j, System.Windows.Threading.DispatcherPriority.Normal, new object[] { }));

                }
                catch (Exception)
                {

                    throw;
                }

            }

        }

    }
}
